package com.vallis.asfc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.asfc.entity.AsfcExam;
import com.vallis.asfc.entity.JoinExamUser;
import com.vallis.asfc.entity.User;
import com.vallis.asfc.service.AsfcExamService;
import com.vallis.asfc.service.JoinExamUserService;
import com.vallis.asfc.service.UserService;
import com.vallis.asfc.util.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-08-13
 */
@RestController
@CrossOrigin
@RequestMapping("/join-exam-user")
public class JoinExamUserController {
    @Autowired
    private AsfcExamService asfcExamService;
    @Autowired
    private UserService userService;
    @Autowired
    private JoinExamUserService joinExamUserService;
    
    @GetMapping("sureAge")
    public Result sureAge(@RequestHeader String Authorization, Long id) {
        
        
        AsfcExam byId = asfcExamService.getById(id);
        Claims vallis = Jwts.parser().setSigningKey("vallis")
                .parseClaimsJws(Authorization).getBody();
        
        
        User e = userService.getById(Long.valueOf(vallis.getId()));
        Integer userAge = e.getUserAge();
        Integer userState = e.getUserState();
        Integer allowOfficeUser = byId.getAllowOfficeUser();
        System.out.println("本用户是否等于空:" + (null == e));
        System.out.println("本考试是否等于空:" + (null == byId));
        System.out.println(e);
        System.out.println(byId);
        JoinExamUser one = joinExamUserService.getOne(new QueryWrapper<JoinExamUser>().eq("user_id", e.getUserId()).eq("exam_id", byId.getExamId()));
        if (null != one) {
            return new Result(400, "本场考试您已报过名", null);
        }
        if (userAge <= byId.getAllowAgeMax() && userAge >= byId.getAllowAgeMin()) {
            return new Result(200, "您的年龄符合此比赛的报名要求", null);
        }
        
        return new Result(400, "您的年龄不符合此比赛的报名要求", null);
    }
    
    @PutMapping("addJoinExamUser")
    public Result addJoinExamUser(@RequestHeader String Authorization, Long id) {
        
        Claims vallis = Jwts.parser().setSigningKey("vallis")
                .parseClaimsJws(Authorization).getBody();
        User x = userService.getById(Long.valueOf(vallis.getId()));
        AsfcExam byId = asfcExamService.getById(id);
        JoinExamUser e = new JoinExamUser();
        e.setUserId(x.getUserId()).setConfirmState(0).setExamId(byId.getExamId()).setExamName(byId.getExamName()).setUserName(x.getUserName()).setUserOrgId(x.getUserDeptId());
        
        boolean save = joinExamUserService.save(e);
        return new Result(200, "报名考试成功", save);
    }
    
    @PutMapping("updateJoinExamUser")
    public Result updateJoinExamUser(@RequestBody JoinExamUser joinExamUser) {
        
        boolean b = joinExamUserService.saveOrUpdate(joinExamUser);
        return new Result(200, "修改成功", b);
    }
    
    @PutMapping("changeJoinExamUserState")
    public Result changeJoinExamUserState(Long jeuId, Integer state) {
        
        JoinExamUser byId = joinExamUserService.getById(jeuId);
        byId.setConfirmState(state);
        
        return new Result(200, "修改成功", joinExamUserService.saveOrUpdate(byId));
    }
    
    
    @PutMapping("RemoveJoinExamUser")
    public Result RemoveJoinExamUser(Long id) {
        boolean b = joinExamUserService.removeById(id);
        return new Result(200, "修改成功", b);
    }
    
    @GetMapping("getThisExamUsers")
    public Result getThisExamUsers(Long id) {
        System.out.println("获取的id为：" + id);
        List<JoinExamUser> joinExamUsers = joinExamUserService.list(new QueryWrapper<JoinExamUser>().eq("exam_id", id));
        
        joinExamUsers.forEach(item -> {
            User byId = userService.getById(item.getUserId());
            item.setOrgName(byId.getUserDeptName());
            item.setAge(byId.getUserAge());
            
        });
        return new Result(200, "获取成功", joinExamUsers);
        
    }
}
